// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Using OOZEggman as a prefix here because there's like 12 "Eggman" objects in the game so its less confusing this way

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.originPos.y
private alias object.value2 : object.oscillationAngle
private alias object.value3 : object.health
private alias object.value4 : object.invincibilityTimer
private alias object.value5 : object.exploding
private alias object.value6 : object.emergeTargetPos
private alias object.value7 : object.highestEmergePos
private alias object.value8 : object.submergeTargetPos
private alias object.value9 : object.spawnHarpoon

// Eggman States
private alias 0 : OOZEGGMAN_AWAITPLAYER
private alias 1 : OOZEGGMAN_STARTBOSS
private alias 2 : OOZEGGMAN_EMERGE
private alias 3 : OOZEGGMAN_LOOKAROUND
private alias 4 : OOZEGGMAN_SUBMERGE
private alias 5 : OOZEGGMAN_HARPOONATTACK
private alias 6 : OOZEGGMAN_CANNONATTACK
private alias 7 : OOZEGGMAN_EXPLODE
private alias 8 : OOZEGGMAN_DEFEATED
private alias 9 : OOZEGGMAN_ESCAPE

// Eggman Animations
private alias 0 : OOZEGGANI_INVISIBLE
private alias 1 : OOZEGGANI_IDLE
private alias 2 : OOZEGGANI_LAUGH
private alias 3 : OOZEGGANI_HIT
private alias 4 : OOZEGGANI_DEFEATED
private alias 5 : OOZEGGANI_TOASTED
private alias 6 : OOZEGGANI_PANIC

// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.animation : player.animation
private alias object.collisionRight : player.collisionRight

private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

private alias  0 : MUSICEVENT_FADETOBOSS
private alias  1 : MUSICEVENT_FADETOSTAGE


// ========================
// Function Declarations
// ========================

reserve function OOZEggman_Oscillate


// ========================
// Function Definitions
// ========================

private function OOZEggman_Oscillate
	Sin256(object.ypos, object.oscillationAngle)
	object.ypos <<= 9
	object.ypos += object.originPos.y
	object.oscillationAngle += 4
	object.oscillationAngle &= 0xFF
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case OOZEGGMAN_AWAITPLAYER
		if player[0].xpos > object.xpos
			temp0 = screen.xcenter
			FlipSign(temp0)
			if temp0 > -192
				temp0 = -192
			end if
			temp0 += object.ixpos
			stage.newXBoundary1 = temp0

			temp0 = screen.xcenter
			if temp0 < 192
				temp0 = 192
			end if
			temp0 += object.ixpos
			stage.newXBoundary2 = temp0

			temp0 = stage.newYBoundary2
			temp0 -= screen.ysize
			stage.newYBoundary1 = temp0

			object.emergeTargetPos = object.ypos
			object.emergeTargetPos -= 0x400000
			object.highestEmergePos = object.ypos
			object.highestEmergePos -= 0x440000
			object.submergeTargetPos = object.ypos
			object.animation = OOZEGGANI_IDLE
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOBOSS, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			
#platform: USE_STANDALONE
			object.health = 8
#endplatform
#platform: USE_ORIGINS
			if game.bossOneLife == false
				object.health = 8
			else
				object.health = 1
			end if
#endplatform
			
			object.priority = PRIORITY_ACTIVE
			object.state = OOZEGGMAN_STARTBOSS
		end if
		break

	case OOZEGGMAN_STARTBOSS
		object.timer++
		if object.timer == 60
			object.timer = 0
			object.yvel = -0x8000
			if player[0].xpos > object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			object.state = OOZEGGMAN_EMERGE
		end if
		break

	case OOZEGGMAN_EMERGE
		object.ypos += object.yvel
		if object.ypos <= object.emergeTargetPos
			object.ypos = object.emergeTargetPos
			object.originPos.y = object.ypos
			object.oscillationAngle = 0
			object.state = OOZEGGMAN_LOOKAROUND
		end if

		if object.animation == OOZEGGANI_IDLE
			object.animationTimer = 0
		end if
		break

	case OOZEGGMAN_LOOKAROUND
		CallFunction(OOZEggman_Oscillate)

		// If we got hit, get outta here!
		object.timer++
		if object.invincibilityTimer > 0
			object.timer = 168
		end if

		if object.timer == 168
			object.timer = 0
			object.yvel = -0x4000
			object.state = OOZEGGMAN_SUBMERGE
		end if
		break

	// This state also manages the harpoon/cannon
	case OOZEGGMAN_SUBMERGE
		object.ypos += object.yvel
		if object.ypos < object.highestEmergePos
			object.ypos = object.highestEmergePos
			object.yvel = 0x8000
		end if

		if object.ypos >= object.submergeTargetPos
			object.ypos = object.submergeTargetPos
			object.yvel = -0x8000
			if player[0].xpos > object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if

			if object.spawnHarpoon == true
				temp0 = object.entityPos
				temp0++
				ResetObjectEntity(temp0, TypeName[Eggman Harpoon], object.direction, object.xpos, object.ypos)
				object[+1].ypos -= 0x300000
				object[+1].priority = PRIORITY_ACTIVE
				object.state = OOZEGGMAN_HARPOONATTACK
			else
				temp0 = object.entityPos
				temp0++
				ResetObjectEntity(temp0, TypeName[Eggman Cannon], object.direction, object.xpos, object.ypos)
				object[+1].ypos -= 0x200000
				object[+1].priority = PRIORITY_ACTIVE
				object.state = OOZEGGMAN_CANNONATTACK
			end if

			object.visible = false
			object.spawnHarpoon = false
		end if

		if object.animation == OOZEGGANI_IDLE
			object.animationTimer = 0
		end if
		break

	// Wait for the harpoon attack to be over, then do the cannon attack
	case OOZEGGMAN_HARPOONATTACK
		if object[+1].type == TypeName[Blank Object]
			temp0 = object.entityPos
			temp0++
			ResetObjectEntity(temp0, TypeName[Eggman Cannon], object.direction, object.xpos, object.ypos)
			object[+1].ypos -= 0x200000
			object[+1].priority = PRIORITY_ACTIVE
			object.state = OOZEGGMAN_CANNONATTACK
		end if
		break

	// Wait for the cannon attack to be over, then emerge
	case OOZEGGMAN_CANNONATTACK
		if object[+1].type == TypeName[Blank Object]
			object.visible = true
			object.state   = OOZEGGMAN_EMERGE
		end if
		break

	case OOZEGGMAN_EXPLODE
		object.timer++
		if object.timer == 180
			object.timer = 0
			object.animation = OOZEGGANI_TOASTED
			object.exploding = false
			object.state  = OOZEGGMAN_DEFEATED
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break

	case OOZEGGMAN_DEFEATED
		object.timer++
		if object.timer == 30
			object.yvel = 0
			object.timer = 0
			object.state = OOZEGGMAN_ESCAPE
		end if
		break

	case OOZEGGMAN_ESCAPE
		if object.yvel < 0x10000
			object.yvel += 0x400
		end if

		object.ypos += object.yvel
		if object.ypos > object.submergeTargetPos
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
		
	end switch

	if object.health != 0
		if object.invincibilityTimer > 0
			object.invincibilityTimer--
			GetBit(temp0, object.invincibilityTimer, 0)
			if temp0 == true
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0x000000)
			end if
		end if

		if object.visible == true
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object.invincibilityTimer < 32
					CheckEqual(player[currentPlayer].animation, ANI_HURT)
					temp0 = checkResult
					CheckEqual(player[currentPlayer].animation, ANI_DYING)
					temp0 |= checkResult
					CheckEqual(player[currentPlayer].animation, ANI_DROWNING)
					temp0 |= checkResult
					if temp0 != 0
						if object.animation != OOZEGGANI_LAUGH
							object.animation = OOZEGGANI_LAUGH
							object.animationTimer = 0
						end if
					end if

					BoxCollisionTest(C_TOUCH, object.entityPos, -24, -10, 24, 24, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
					if checkResult == true
						CallFunction(Player_CheckHit)
						if checkResult == true
							object.health--
							if object.health == 0
#platform: USE_ORIGINS
								CallNativeFunction2(NotifyCallback, NOTIFY_KILL_BOSS, 0)
								CallNativeFunction2(NotifyCallback, NOTIFY_BOSS_END, 1)
#endplatform
								
								player.score += 1000
								object.timer 			= 0
								object.animation 		= OOZEGGANI_DEFEATED
								object.animationTimer 	= 0
								object.exploding 		= true
								object.originPos.y 		= object.ypos
								object.state			= OOZEGGMAN_EXPLODE
								SetPaletteEntry(0, 192, 0x000000)
							else
								object.animation 			= OOZEGGANI_HIT
								object.animationTimer 		= 0
								object.invincibilityTimer 	= 64
								object.spawnHarpoon 		= true
								PlaySfx(SfxName[Boss Hit], false)
							end if
						end if
					end if
				end if
			next
		end if
	end if

	// Handle Right Stage Boundary
	if object.state != OOZEGGMAN_ESCAPE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += player[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				player[currentPlayer].xvel = 0
				player[currentPlayer].speed = 0
				player[currentPlayer].xpos = temp1
				temp0 = player[currentPlayer].collisionRight
				temp0 <<= 16
				player[currentPlayer].xpos -= temp0
			end if
		next
	end if

	// Exploding stuff
	if object.exploding == true
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos

			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos

			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], false)
		end if
	end if
end event


event ObjectDraw
	// Submarine
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)

	switch object.animation
	default
	case OOZEGGANI_INVISIBLE
		break

	case OOZEGGANI_IDLE
		temp0 = object.animationTimer
		temp0 /= 40
		object.animationTimer++
		object.animationTimer %= 80
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case OOZEGGANI_LAUGH
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if player[0].animation != ANI_DYING
				object.animation = OOZEGGANI_IDLE
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case OOZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = OOZEGGANI_IDLE
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break

	case OOZEGGANI_DEFEATED
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case OOZEGGANI_TOASTED
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break

	case OOZEGGANI_PANIC
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch
end event


event ObjectStartup
	SetPaletteEntry(0, 192, 0x000000) // This is already reset in [OOZSetup]... does it really need to be done again here?
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 12, 462, 118)	// Eggman (Idle) 	 - #0
		SpriteFrame(-12, -12, 24, 12, 487, 118)	// Eggman (Looking)  - #1
		SpriteFrame(-12, -12, 24, 12, 462, 131)	// Eggman (Laugh 1)  - #2
		SpriteFrame(-12, -12, 24, 12, 487, 131)	// Eggman (Laugh 2)  - #3
		SpriteFrame(-12, -12, 24, 12, 462, 144)	// Eggman (Hit) 	 - #4
		SpriteFrame(-12, -12, 24, 12, 487, 144)	// Eggman (Defeated) - #5
		SpriteFrame(-48, -32, 96, 64, 365, 125) // Submarine 		 - #6

		if options.vsMode == true
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 12, 413, 91) 	// Eggman (Idle) 	 - #0
		SpriteFrame(-12, -12, 24, 12, 438, 91) 	// Eggman (Looking)  - #1
		SpriteFrame(-12, -12, 24, 12, 413, 104)	// Eggman (Laugh 1)  - #2
		SpriteFrame(-12, -12, 24, 12, 438, 104)	// Eggman (Laugh 2)  - #3
		SpriteFrame(-12, -12, 24, 12, 413, 117)	// Eggman (Hit) 	 - #4
		SpriteFrame(-12, -12, 24, 12, 438, 117)	// Eggman (Defeated) - #5
		SpriteFrame(-48, -32, 96, 64, 313, 112) // Submarine 		 - #6
	end if
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(1) // Sub
	DrawSprite(0) // Eggman
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 12, 462, 118)	// Eggman (Idle) 	 - #0
		SpriteFrame(-48, -32, 96, 64, 365, 125) // Submarine 		 - #1
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 12, 413, 91) 	// Eggman (Idle) 	 - #0
		SpriteFrame(-48, -32, 96, 64, 313, 112) // Submarine 		 - #1
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
